package com.sleep.getexcel.service.impl;

import com.mysql.cj.util.StringUtils;
import com.sleep.getexcel.dao.KpiLabelDataMapper;
import com.sleep.getexcel.pojo.KpiLabelData;
import com.sleep.getexcel.service.KpiLabelDataService;
import com.sleep.getexcel.utils.ReadExcel;
import com.sleep.getexcel.vo.FilePath;
import com.sleep.getexcel.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Sleep-6000
 * @date 2022/8/8
 */
@Service
@Transactional
public class KpiLabelDataServiceImpl implements KpiLabelDataService {
    @Autowired
    private KpiLabelDataMapper kpiLabelDataMapper;

    @Override
    public Result insertData(FilePath filePath) {
        String readPath = filePath.getReadPath();
        if (StringUtils.isNullOrEmpty(readPath)) {
            return Result.fail(10003, "参数错误");
        }
        ReadExcel readExcel = new ReadExcel();
        if (!readExcel.preReadCheck(readPath)) {
            return Result.fail(10001, "文件不存在");
        }
        List<List<String>> read = readExcel.read(readPath);
        if (read.size() < 2) {
            return Result.fail(10002, "文件为空");
        }
        int success = 0;
        for (List<String> strings : read) {
            KpiLabelData labelData = new KpiLabelData();
            labelData.setLabelCode(strings.get(0));
            labelData.setLabelValue(BigDecimal.valueOf(1));
            labelData.setTimeGrading("EVER");
            labelData.setHaveCalculate(0);
            labelData.setDataState(0);
            labelData.setDataVersion(1);

            success += kpiLabelDataMapper.insert(labelData);
        }
        return Result.success(success);
    }
}
